#include "Login.h"
#include "MySqlite.h"
#include <QApplication>
#include <QDebug>
#include <QMessageBox>
#include "mainwindow.h"
MySqlite::MySqlite(QObject *parent) : QObject(parent)
{

}

void MySqlite::readConfig()
{
    this->queryLock.lock();

    QString str="SELECT ip,port from server";
    if(this->query.exec(str))
    {
        if(query.next())
        {
            Login::myTCPClient->getIp()=query.value(0).toString();
            Login::myTCPClient->getPort()=query.value(1).toInt();
        }
    }

    str="SELECT pointer from surface_opinter where surfaceName='operatorData'";
    if(this->query.exec(str))
    {
        if(query.next())
        {
            MainWindow::mySqlite->operatorDataPoint=query.value(0).toInt();
            qDebug()<<"MainWindow::mySqlite->operatorDataPoint:"<<MainWindow::mySqlite->operatorDataPoint<<endl;
        }
    }
    str="SELECT pointer from surface_opinter where surfaceName='operatorNumber'";
    if(this->query.exec(str))
    {
        if(query.next())
        {
            MainWindow::mySqlite->operatorNumber=query.value(0).toInt();
            qDebug()<<"MainWindow::mySqlite->operatorNumber:"<<MainWindow::mySqlite->operatorNumber<<endl;
        }
    }
    this->queryLock.unlock();

}

bool MySqlite::connectSql()
{
    this->mysqlite=QSqlDatabase::addDatabase("QSQLITE","mysql");
    mysqlite.setDatabaseName("pump.db");
    if(mysqlite.open())
    {
        this->query=QSqlQuery(this->mysqlite);
        qDebug()<<"open succend"<<endl;
        readConfig();
        //set20000Data();
        return true;
    }
    else
    {
       //重新打开
        QMessageBox* box=new QMessageBox;
        box->warning(nullptr,"","连接服务器失败，请重新打开");
        QApplication::quit();
        return false;
    }
}

void MySqlite::set20000Data()
{
    this->queryLock.lock();

    QString str="INSERT INTO operatorData (operatorNumber, data, time) VALUES ('', '', '');";
    for(int i=0;i<20000;i++)
    {qDebug()<<"....."<<endl;
        this->query.exec(str);
    }
    this->queryLock.unlock();
}

void MySqlite::addData(operatorData p)
{
    QMutexLocker(&this->queryLock);
    this->datas.append(p);
}

void MySqlite::updataOperatorNumber()
{
    this->operatorNumber++;
    QString str="UPDATE surface_opinter SET pointer=%1 WHERE surfaceName='operatorNumber'";
    this->query.exec(str.arg(this->operatorNumber));
}

void MySqlite::insertDataToSql()
{qDebug()<<"MySqlite thread:"<<QThread::currentThreadId()<<endl;
//    QMutexLocker(&this->queryLock);
//    QString str1="UPDATE operatorData SET operatorNumber = %1 ,data=%2 ,time='%3' WHERE sequence =%4;";
//    QString str2="UPDATE surface_opinter SET pointer=%1 WHERE surfaceName='operatorData'";
//    for(int i=0;i<this->datas.size();i++)
//    {
//        qDebug()<<this->query.exec(str1.arg(datas[i].operatorNumber).arg(datas[i].data).arg(datas[i].time).arg(MainWindow::mySqlite->operatorDataPoint++))<<endl;
//        if(MainWindow::mySqlite->operatorDataPoint>20000)
//            MainWindow::mySqlite->operatorDataPoint=1;
//        qDebug()<<"point:"<<this->query.exec(str2.arg(MainWindow::mySqlite->operatorDataPoint))<<endl;
//    }
//    datas.clear();
    QMutexLocker(&this->queryLock);
    QString str1="replace into operatorData(sequence,operatorNumber,data,time) VALUES ";
    QString str2="UPDATE surface_opinter SET pointer=%1 WHERE surfaceName='operatorData'";
    QString s="(%1,%2,%3,'%4'),";
    for(int i=0;i<this->datas.size();i++)
    {
        str1=str1.append(s.arg(MainWindow::mySqlite->operatorDataPoint++).arg(datas[i].operatorNumber).arg(datas[i].data).arg(datas[i].time));
        if(MainWindow::mySqlite->operatorDataPoint>20000)
            MainWindow::mySqlite->operatorDataPoint=1;
    }
    str1.replace(str1.length()-1,1,";");
    qDebug()<<this->query.exec(str1)<<endl;
    qDebug()<<"point:"<<this->query.exec(str2.arg(MainWindow::mySqlite->operatorDataPoint))<<endl;
}

void MySqlite::clearDatas()
{
    QMutexLocker(&this->queryLock);
    this->datas.clear();
}

int MySqlite::getOperatorNumber()
{
    QMutexLocker(&this->queryLock);
    return this->operatorNumber;
}

MySqlite::~MySqlite()
{
    this->mysqlite.close();
}
